package oop.core.med;
import oop.core.GameLoop;
import oop.core.RunGame;
import oop.core.Scheduler;
import oop.core.Updateable;
import oop.core.entity.*;
import java.util.List;

public class DeathMediator implements Updateable{

	private List<Entity> entities;
	private Avatar avatar;
	//private RunGame rungame;
	private PauseMediator pauseMediator;
	
	public DeathMediator(List<Entity> entities, Avatar avatar, Scheduler scheduler, PauseMediator pauseMediator){
		this.entities = entities;
		this.avatar = avatar;
		this.pauseMediator = pauseMediator;
		scheduler.registerPersistent( this );
	}
	
	public void update()
	{
		avatarDeathCheck();
		mobDeathCheck();
	}
	
	//checks for dead entities and awards Avatar with EXP.
	//required changes:  entities needed .getGivenEXP, avatar needed .gainEXP, added Avatar attribute
	public void mobDeathCheck(){
		for(int i=0; i<entities.size(); i++ ){
			if (entities.get(i).getHP()<=0){
				avatar.gainEXP(entities.get(i).getGivenEXP());
				entities.remove(i);
				i--;
			}
		}
	}
	
	public void avatarDeathCheck(){
		if(avatar.getHP()<=0){
			if(avatar.getLives()<=0){
				pauseMediator.pause();
				RunGame.endGame();
			}
			avatar.takeLives(1);
			avatar.heal(avatar.getDerStats().getMaxHP());
		}
	}

}

 
